package cn.hjc.service;

import cn.hjc.dao.DeptDao;
import cn.hjc.dao.EmpDao;
import cn.hjc.entity.Dept;
import cn.hjc.entity.Emp;
import cn.hjc.entity.EmpCondition;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;
import sun.rmi.runtime.Log;

import java.util.Iterator;
import java.util.List;

public class EmpBiz {
    private EmpDao empDao = new EmpDao();



    public List<Emp> findEmpsByConditions(Object[] conditions) {
        Transaction tx = null;
        List<Emp> emps = null;
        try {
            tx = empDao.currentSession().beginTransaction();//开始事务
            emps = empDao.findByConditions(conditions);//持久化操作
            tx.commit();
            ;//提交事务
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();//回滚事务
        }
        return emps;
    }

    public List<Emp> findEmpsByConditions(EmpCondition conditions) {
        Transaction tx = null;
        List<Emp> emps = null;
        try {
            tx = empDao.currentSession().beginTransaction();//开启事务
            //HQL根据条件动态生成
            StringBuilder hql = new StringBuilder("from Emp as emp where 1=1");
            if (conditions.getJob() != null && conditions.getJob().length() > 0) {
                hql.append(" and emp.job = :job");
            }
            if (conditions.getSalary() != null && conditions.getSalary() != 0) {
                hql.append(" and emp.sal > :salary");
            }
            if (null != conditions.getHireDateStart()) {
                hql.append(" and emp.hiredate > :hireDateStart");
            }
            if (null != conditions.getHireDateEnd()) {
                hql.append(" and emp.hiredate < :hireDateEnd");
            }
            emps = new EmpDao().findByConditions(hql.toString(), conditions);
            tx.commit();//提交事务
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();//回滚事务
        }
        return emps;
    }

    public Long countBySalary(Double sal) {
        Transaction tx = null;
        Long result = null;
        try {
            tx = empDao.currentSession().beginTransaction();//开启事务
            result = empDao.obtainTheRowCount(sal);
            tx.commit();//提交事务
        } catch (HibernateException e) {
            e.printStackTrace();
            if (tx != null)
                tx.rollback();//回滚事务
        }
        return result;
    }

    public  List<Emp> findEmpsByPage(int pageNo,int pageSize){
        Transaction tx = null;
        List<Emp> emps = null;
        try {
            tx = empDao.currentSession().beginTransaction();//开启事务
            emps = new EmpDao().findByPage(pageNo,pageSize);
            tx.commit();//提交事务
        }catch (HibernateException e){
            e.printStackTrace();
            if(tx !=null)
                tx.rollback();//回滚事务

        }
        return  emps;
    }

    public void addNewEmp(Emp emp){
        Transaction tx = null;
        try {
            tx = empDao.currentSession().beginTransaction();//开始事物
            empDao.save(emp);
            tx.commit();//提交事物
        }catch (HibernateException e){
            e.printStackTrace();
            if(tx != null)
                tx.rollback();//回滚事务
        }

    }

    public List<Emp> findEmpsByDept(Dept dept){
        Transaction tx = null;
        List<Emp> result = null;
        try {
            tx = empDao.currentSession().beginTransaction();//开启事务
            result = empDao.findByDept(dept);
            tx.commit();//提交事务
        }catch (HibernateException e){
            e.printStackTrace();
            if(tx !=null)
                tx.rollback();
        }
        return  result;
    }

    public List<Emp> findAllEmps(){
        Transaction tx = null;
        List<Emp> result = null;
        try {
            tx = empDao.currentSession().beginTransaction();//-*开启事务
            result = empDao.findAll();
            //测试对象间导航效果，注意须在会话关闭钱测试查询效果，
            //原因会在下文的延迟加载一节进行分析
            for(Emp emp : result){
                System.out.println("员工姓名:"+emp.getEmpName()+"\t");
                System.out.println("所在部门:"+emp.getDept().getDeptName());
            }
            tx.commit();//提交事务
        }catch (HibernateException e){
            e.printStackTrace();
            if(tx !=null)
                tx.rollback();//-回滚事务
        }
        return  result;
    }

    public void changeDept(Integer empNo,Integer deptNo){
        Transaction tx = null;
        try {
            tx = empDao.currentSession().beginTransaction();//开启事务
            /*加载Dept和Emp的持久化对象*/
            Dept dept = new DeptDao().load(deptNo);
            Emp emp = empDao.load(empNo);
            /*建立Dept对象和Emp对象的关联关系*/
            emp.setDept(dept);
            dept.getEmps().add(emp);
            tx.commit();//提交事务
        }catch (HibernateException e){
            e.printStackTrace();
            if(tx !=null)
                tx.rollback();//回滚事务
        }
    }
}
